package com.xin.web;

import com.xin.pojo.Book;
import com.xin.pojo.Page;
import com.xin.service.BookService;
import com.xin.service.impl.BookServiceimpl;
import com.xin.utils.WebUtils;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;

public class BookServlet extends BaseServlet{
    BookService bookService=new BookServiceimpl();

    /**
     * 从数据库中获取图书，显示到页面上
     * @param req
     * @param resp
     * @throws ServletException
     * @throws IOException
     */
    protected void list(HttpServletRequest req, HttpServletResponse resp) throws ServletException,
            IOException{
        List<Book> books=bookService.queryBook();
        req.setAttribute("books",books);
        req.getRequestDispatcher("/pages/manager/book_manager.jsp").forward(req,resp);
    }

    /**
     * 添加图书
     * @param req
     * @param resp
     * @throws ServletException
     * @throws IOException
     */
    protected void add(HttpServletRequest req,HttpServletResponse resp) throws ServletException,IOException{
        Book book = WebUtils.copyParamToBean(req.getParameterMap(), new Book());
        bookService.addBook(book);
        String pageNo;
        if (req.getParameter("lastpage")==null){
            pageNo = req.getParameter("pageNo");
        }else{
            pageNo =req.getParameter("lastpage");
        }
        resp.sendRedirect(req.getContextPath()+"/manager/bookServlet?action=page2&pageNo="+pageNo);
    }
    protected void getBook(HttpServletRequest req,HttpServletResponse resp) throws ServletException,IOException{
           int i=Integer.parseInt(req.getParameter("id"));
        Book book = bookService.queryBookById(i);
        req.setAttribute("book",book);
        req.getRequestDispatcher("/pages/manager/book_edit.jsp").forward(req,resp);
    }

    /**
     *
     * @param req
     * @param resp
     * @throws ServletException
     * @throws IOException
     */
    protected void update(HttpServletRequest req,HttpServletResponse resp) throws ServletException,IOException{
        Book book = WebUtils.copyParamToBean(req.getParameterMap(), new Book());
        System.out.println(book);
        bookService.update(book);
        resp.sendRedirect(req.getContextPath()+"/manager/bookServlet?action=page2&pageNo="+req.getParameter("pageNo"));
    }

    /**
     *从数据库中删除某本书
     * @param req
     * @param resp
     * @throws ServletException
     * @throws IOException
     */
    protected void delete(HttpServletRequest req,HttpServletResponse resp) throws ServletException,IOException{
        String id = req.getParameter("id");
        int i = Integer.parseInt(id);
        bookService.deleteBookById(i);
        int pageNo = WebUtils.parseInt(req.getParameter("pageNo"), 1);
        Page<Book> pagetest = bookService.page2(pageNo, Page.PAGE_SIZE);
        if (!pagetest.getItems().isEmpty()){
            resp.sendRedirect(req.getContextPath()+"/manager/bookServlet?action=page2&pageNo="+pageNo);
        }else{
            pageNo--;
            resp.sendRedirect(req.getContextPath()+"/manager/bookServlet?action=page2&pageNo="+pageNo);
        }

    }
//    protected void text(HttpServletRequest req,HttpServletResponse resp) throws ServletException,IOException{
//        Map<String, String[]> parameterMap = req.getParameterMap();
//        for (String key:parameterMap.keySet()){
//            System.out.println(key);
//            for (String s:parameterMap.get(key)){
//                System.out.println(s);
//            }
//
//        }
//        System.out.println(req.getParameter("id"));
//    }
//    protected void page(HttpServletRequest req,HttpServletResponse resp) throws ServletException,IOException{
//        System.out.println("过来了");
//        int pageNo =WebUtils.parseInt(req.getParameter("pageNo"),0);
//        int pageSize =WebUtils.parseInt(req.getParameter("pageSize"),Page.PAGE_SIZE);
//        Page<Book> page = bookService.page(pageNo, pageSize);
//       req.setAttribute("page",page);
//        req.getRequestDispatcher("/pages/manager/book_manager.jsp").forward(req,resp);
//    }

    protected void page2(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        int pageNo= WebUtils.parseInt(req.getParameter("pageNo"), 1);
        int pageSize = WebUtils.parseInt(req.getParameter(String.valueOf(Page.PAGE_SIZE)), 4);
        Page<Book> page = bookService.page2(pageNo, pageSize);
        page.setUrl("manager/bookServlet?action=page2");
        req.setAttribute("page",page);
        req.getRequestDispatcher("/pages/manager/book_manager.jsp").forward(req,resp);
    }
//    protected void getLastpage(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
//        req.setAttribute("lastpage",req.getParameter("lastpage"));
//        req.getRequestDispatcher("/pages/manager/book_edit.jsp").forward(req,resp);
//    }

}
